<?php

require_once 'Zend/Controller/Action.php';

/**
 * @param string $who
 * @param string $when
 * @return string
 */
function getWords($who, $when)
{
    return "Hello $who, Good $when";
}

class RestClass
{
    public function getName($id) {
        if ($id == 1) {
            return array('name' => 'name 1 returned.');
        } else {
            return array('name' => '$id != 1 !!');
        }
    }
}

class RestController extends Zend_Controller_Action 
{
    public function indexAction() 
    {
        require_once('Zend/Rest/Server.php');
        $server = new Zend_Rest_Server();
        $this->getFrontController()->setParam('noViewRenderer', TRUE);
        $server->setClass('RestClass');
        $server->addFunction('getWords');
        $server->handle();
        exit();
    }

    public function clientAction() {
        require_once('Zend/Rest/Client.php');
        try {
            $client = new Zend_Rest_Client($this->view->host . '/rest/');
            $response = $client->getName(1, 'tt')->get();
            if ($response->isSuccess()) {
                echo $response->name . '<br />';
            }

            $response = $client->getWords('kim', 'woooo')->get();
            if ($response->isSuccess()) {
                echo $response;
                die();
            }

        } catch (Zend_Rest_Client_Exception $e) {
            echo $e->getCode() . ': ' . $e->getMessage() . "\n";
        }
    }
}

